﻿using BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace Hospital.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WardController : ControllerBase
    {
        /// <summary>
        /// 依赖注入WardBLL
        /// </summary>
        WardBLL bll;
        ILogger<WardController> logger;

        public WardController(WardBLL bll, ILogger<WardController> logger)
        {
            this.bll = bll;
            this.logger = logger;
        }

        /// <summary>
        /// 添加科室
        /// </summary>
        /// <param name="w">科室实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> AddWard(WardModel w)
        {
            try
            {
                logger.LogInformation($"在{DateTime.Now}添加了一个{w.WardName}的科室");
                return await bll.AddWard(w);
            }
            catch (Exception ex)
            {
                logger.LogError("添加失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 显示科室列表
        /// </summary>
        /// <param name="wardName">科室名称</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public IQueryable<WardModel> ShowWard(string? wardName = "")
        {
            try
            {
                logger.LogInformation($"在{DateTime.Now}显示科室列表成功");
                return bll.ShowWard(wardName);
            }
            catch (Exception ex)
            {
                logger.LogError("显示失败", ex.Message);
                throw;
            }
        }
    }
}
